package com.zapi.notification.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zapi.notification.entity.Message;
import com.zapi.notification.mapper.MessageMapper;
import com.zapi.notification.service.MessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 消息Service实现类
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Override
    @Transactional
    public boolean sendMessage(Message message) {
        // 设置默认值
        if (message.getStatus() == null) {
            message.setStatus(0); // 未读
        }
        if (message.getCreateTime() == null) {
            message.setCreateTime(LocalDateTime.now());
        }
        
        return save(message);
    }

    @Override
    @Transactional
    public int sendBatchMessages(List<Message> messages) {
        // 设置默认值
        for (Message message : messages) {
            if (message.getStatus() == null) {
                message.setStatus(0); // 未读
            }
            if (message.getCreateTime() == null) {
                message.setCreateTime(LocalDateTime.now());
            }
        }
        
        return saveBatch(messages) ? messages.size() : 0;
    }

    @Override
    public Page<Message> getMessagesByReceiverId(Long receiverId, Integer current, Integer size) {
        Page<Message> page = new Page<>(current, size);
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id", receiverId)
                   .orderByDesc("create_time");
        
        return page(page, queryWrapper);
    }

    @Override
    public Page<Message> getMessagesBySenderId(Long senderId, Integer current, Integer size) {
        Page<Message> page = new Page<>(current, size);
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sender_id", senderId)
                   .orderByDesc("create_time");
        
        return page(page, queryWrapper);
    }

    @Override
    @Transactional
    public boolean markAsRead(Long messageId, Long receiverId) {
        UpdateWrapper<Message> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", messageId)
                    .eq("receiver_id", receiverId)
                    .set("status", 1) // 已读
                    .set("read_time", LocalDateTime.now());
        
        return update(updateWrapper);
    }

    @Override
    @Transactional
    public int markBatchAsRead(List<Long> messageIds, Long receiverId) {
        UpdateWrapper<Message> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", messageIds)
                    .eq("receiver_id", receiverId)
                    .set("status", 1) // 已读
                    .set("read_time", LocalDateTime.now());
        
        return update(updateWrapper) ? messageIds.size() : 0;
    }

    @Override
    public int getUnreadCount(Long receiverId) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id", receiverId)
                   .eq("status", 0); // 未读
        
        return (int) count(queryWrapper);
    }

    @Override
    @Transactional
    public boolean deleteMessage(Long messageId, Long userId) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", messageId)
                   .and(wrapper -> wrapper.eq("sender_id", userId)
                                        .or()
                                        .eq("receiver_id", userId));
        
        return remove(queryWrapper);
    }

    @Override
    @Transactional
    public int deleteBatchMessages(List<Long> messageIds, Long userId) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", messageIds)
                   .and(wrapper -> wrapper.eq("sender_id", userId)
                                        .or()
                                        .eq("receiver_id", userId));
        
        return remove(queryWrapper) ? messageIds.size() : 0;
    }
} 